package com.twg.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.twg.entity.user;
import com.twg.util.utilJDBC;


public class UsertDao {
	//��ѯ����
	public List selAll(){
		String sql="select * from user";
		Object[] obj={};
		ResultSet rs=utilJDBC.query(sql, obj);
		List list= new ArrayList();
		try {
			while(rs.next()){
				user u=new user(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getInt(6), rs.getString(7), rs.getString(8));
				list.add(u);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			utilJDBC.close();
		}
		
		
		return list;
	}
	//���
	public int add(user u){
		
		String sql="insert into user values(null,?,?,?,?,?,?,?)";
		Object[] obj={u.getName(),u.getUser(),u.getPwd(),u.getSex(),u.getAge(),u.getXp(),u.getTx()};
		int i=utilJDBC.update(sql, obj);
		return i;
	}
	//�޸�
	/*public int gai(product p){
		String sql="update product set p_id=?,p_name=?,p_price=?,p_count=? where p_id=?";
		Object[] obj={p.getId(),p.getName(),p.getPrice(),p.getCount(),p.getId()};
		int i=utilJDBC.update(sql, obj);
		return i;
	}
	//ɾ��
		public int del(int id){
			String sql="delete from product where p_id=?";
			Object[] obj={id};
			int i=utilJDBC.update(sql, obj);
			return i;
		}
	//���ID��ѯ
		public product byid(int id){
			String sql="select * from product where p_id=?";
			Object[] obj={id};
			ResultSet rs=utilJDBC.query(sql, obj);
			product p=null;
			try {
				if(rs.next()){
					p=new product(Integer.parseInt(rs.getString(1)), rs.getString(2), rs.getInt(3), rs.getInt(4));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				utilJDBC.close();
			}
			return p;
		}
	//���Name��ѯ
			public product byName(String name){
				String sql="select * from product where p_name=?";
				Object[] obj={name};
				ResultSet rs=utilJDBC.query(sql, obj);
				product p=null;
				try {
					if(rs.next()){
						p=new product(Integer.parseInt(rs.getString(1)), rs.getString(2), rs.getInt(3), rs.getInt(4));
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					utilJDBC.close();
				}
				return p;
			}	
	//ģ���ѯ
			public List MoHuCX(String str){
				String sql="select * from product where p_name like ?";
				Object[] obj={"%"+str+"%"};
				ResultSet rs=utilJDBC.query(sql, obj);
				List list= new ArrayList();
				try {
					while(rs.next()){
						product p=new product(Integer.parseInt(rs.getString(1)), rs.getString(2), rs.getInt(3), rs.getInt(4));
						list.add(p);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					utilJDBC.close();
				}
				
				
				return list;
			}		
	//��ҳ��ѯ
			public List fenyeCX(int page,int pagesize){
				String sql="select * from product limit ?,?";
				Object[] obj={page,pagesize};
				ResultSet rs=utilJDBC.query(sql, obj);
				List list= new ArrayList();
				try {
					while(rs.next()){
						product p=new product(Integer.parseInt(rs.getString(1)), rs.getString(2), rs.getInt(3), rs.getInt(4));
						list.add(p);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					utilJDBC.close();
				}
				
				
				return list;
			}
			
			
			
	//����
	public static void main(String[] args) {
		productDao pd=new productDao();
		List list=pd.fenyeCX(2, 3);
		for(int i=0;i<list.size();i++){
			product p=(product) list.get(i);
			System.out.println(p.getName());
		}
	}*/
}

